package com.sky.task;


import com.github.pagehelper.Page;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.vo.OrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Component
@Slf4j
public class MyTask {

    @Autowired
    private OrderMapper orderMapper;
    @Scheduled(cron = "0 * * * * *")
    public void executeTask(){
        List<Orders> orders = orderMapper.getUnpaidOrders();
        for (Orders order:orders){
            if (order.getOrderTime().plusMinutes(15).isBefore(LocalDateTime.now())){
                Long id = order.getId();
                orderMapper.update(Orders.builder().id(id).status(Orders.CANCELLED).cancelReason("订单超时").cancelTime(LocalDateTime.now()).build());
            }
        }
    }

    @Scheduled(cron = "0 0 0 * * ?")
    public void executeTask1(){
        List<Orders> orders = orderMapper.getDeliveredOrders();
        for (Orders order:orders){
            orderMapper.update(Orders.builder().id(order.getId()).status(Orders.COMPLETED).deliveryTime(LocalDateTime.now()).build());
        }
    }
}
